<?php
namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;

class Caiwu extends Base
{
    public function initialize()
    {
        parent::initialize();
        //钱包模型
        $money_type = input('money_type')?:'money';
        $this->_mod = Db::name($money_type.'_log');
        View::assign('money_type',$money_type);

        //钱包
        View::assign('wallet',logic('Wallet')->getName());
        $tx = logic('Wallet')->getTx();
        View::assign('is_tx',$tx[$money_type]);

        //获取操作类型
        View::assign('leixing',$this->_mod->group('action')->column('action'));
    }

    /**
     * 余额记录
     */
    public function money_log(){
        $money_type = input('money_type')?:'money';
        $table_name = $money_type.'_log';
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('user_id')){
                $map['l.user_id'] = input('user_id');
            }
            if(input('username')){
                $map['u.username'] = input('username');
            }
            if(input('time_start') && input('time_end')){
                $s_time = strtotime(input('time_start'));
                $e_time = strtotime(input('time_end'));
                $map['l.w_time'] = ['between',[$s_time,$e_time]];
            }
            $count = Db::name($table_name)->alias('l')->join('users u','l.user_id=u.user_id')->where($map)->count();

            $field = 'l.*,u.username,u.nickname';
            $list = Db::name($table_name)
                ->alias('l')
                ->join('users u','l.user_id=u.user_id')
                ->field($field)
                ->where($map)
                ->order('l.id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                //$v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $list[$k] = $v;
            }
            $total = Db::name($table_name)
                ->alias('l')
                ->join('users u','l.user_id=u.user_id')
                ->where($map)
                ->field('sum(money) total_money')
                ->find();
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list,'totalRow'=>$total]);
        }
        View::assign('money_type',$money_type);
        return View::fetch();
    }
    /**
     * 商家货款记录
     */
    public function s_money_log(){
        $table_name = 's_money_log';
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if($this->sid){
                $map['l.user_id'] = $this->sid;
            }
            if(input('user_id')){
                $map['l.user_id'] = input('user_id');
            }
            if(input('username')){
                $map['u.username|u.mobile'] = input('username');
            }
            $count = Db::name($table_name)->alias('l')->where($map)->count();

            $field = 'l.*,u.username,u.name nickname';
            $list = Db::name($table_name)
                ->alias('l')
                ->join('suppliers u','l.user_id=u.id')
                ->field($field)
                ->where($map)
                ->order('l.id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                //$v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $list[$k] = $v;
            }
            $total = Db::name($table_name)
                ->alias('l')
                ->where($map)
                ->field('sum(money) total_money')
                ->find();
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list,'totalRow'=>$total]);
        }
        View::assign('sid',$this->sid);
        return View::fetch();
    }

    /*
     * 提现设置
     */
    public function txset(){
        //货币类型
        $money_type = input('money_type');
        $tx = logic('Wallet')->getTx();
        if(IS_POST){
            $data = input('post.');

            unset($data['money_type']);
            $param = [
               'tx_'.$money_type=>$data,
               'tx'=>$tx,
            ];
            tpCache('wallet',$param);
            return json(['status'=>1,'msg'=>'设置成功']);
        }else{
            $config = tpCache('wallet.tx_'.$money_type);
            !isset($config['start']) && $config['start'] = $tx[$money_type];
            View::assign('config',$config);

            //获取提现方式
            $tx_type = logic('Wallet')->getTxType();
            View::assign('tx_type',$tx_type);

            return View::fetch();
        }
    }

    public function export_order()
    {
        set_time_limit(0);
        ini_set('memory_limit','500m');
        //搜索条件
        $where = $this->_search();
        $username = input('username');
        if($username){
            $user_id = Db::name('Users')->where(['username'=>$username])->value('user_id');
            if(!$user_id){
                $where['user_id'] = 0;
            }else{
                $where['user_id'] = $user_id;
            }
        }
        $model = $this->_mod;
        $group = ['id'=>'ID','user_id'=>'会员ID','username'=>'会员','money'=>'金额','yue'=>'余额','action'=>'类型','desc'=>'备注','w_time'=>'操作时间'];
        $group1 = ['id'=>'ID','user_id'=>'会员ID','money'=>'金额','yue'=>'余额','action'=>'类型','desc'=>'备注','w_time'=>'操作时间'];
        $fields = implode(",",array_keys($group1));
        $lists = $model->where($where)->field($fields)->select();
        if($lists){
            $wallet = logic('Wallet')->getName();
            $user = Db::name('Users')->getField('user_id,username');
            foreach($lists as $k=>$v){
                //$v['username'] = '';
                $lists[$k]['username'] = $user[$lists[$k]['user_id']];
                foreach($v as $field=>$value){

                    //时间戳转化
                    if(strstr($field,'time')){
                        $lists[$k][$field] = date('Y-m-d H:i:s',$value);
                    }elseif($field=='username'){
                        $lists[$k][$field] = $user[$lists[$k]['user_id']];
                    }
                }
            }
            //dump($lists);exit;

            $money_type = I('money_type');
            $this->export($group,$lists,$wallet[$money_type].'财务明细');
        }else{
            $this->error('暂无数据');
        }
    }
}